package com.nd.android.musicboxlibrary.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import com.common.utils.StarAppUtils;

import com.common.StarAppConfig;
import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.ui.common.UnMixable;
import com.nd.android.musicboxlibrary.ui.vip.VipUtil;

/**
 * 
 * <br>
 * Created 2014年9月15日 下午11:06:35
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
public class PlayListAdapter extends AbstractAdapter<MusicInfo, PlayListViewHolder> implements
        OnItemClickListener {

    private long playId;

    /**
     * Creates a new instance of PlayListAdapter. <br>
     * Created 2014年9月15日 下午11:06:40
     * 
     * @param context Context
     */
    public PlayListAdapter(Context context) {
        super(context);
        playId = getCurrentPlayId();
    }

    /**
     * 获取当前播放id <br>
     * Created 2014年11月8日 下午11:30:02
     * 
     * @return long
     * @author wat.ztag
     */
    private long getCurrentPlayId() {
        MusicInfo musicInfo = ManagerFactory.getPlayerInstance().getCurrentMusicInfo();
        if (musicInfo == null) {
            return 0;
        } else {
            return musicInfo.getId();
        }
    }

    @Override
    protected PlayListViewHolder getNewHolder() {
        return new PlayListViewHolder();
    }

    @Override
    protected int getItemLayout() {
        return R.layout.mbx_play_list_item;
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:06:43
     * 
     * @author wat.ztag
     */
    public void setPlayId() {
        playId = getCurrentPlayId();
        notifyDataSetChanged();
    }

    @Override
    public void bindView(PlayListViewHolder holder, MusicInfo data, int position) {
        holder.txtName.setText(data.getMusicName());
        holder.txtSinger.setText(data.getSingerName() + " · " + data.getSpecialName());
        holder.txtTime.setText(data.getTime());
        holder.txtMV.setVisibility(data.isHasMv() ? View.VISIBLE : View.GONE);
        if(StarAppConfig.getBooleanConfig(StarAppConfig.CONFIG_VERSION1_8)){
			//有HQid ,hq文件以及下载完了，再显示HQ标志 by jiangwb
			if(data.getHQId() > 0 && VipUtil.fileExist(data.getHQMusicPath())){
				holder.txtHQ.setBackgroundColor(mContext.getResources().getColor(R.color.mbx_font_lyric_pink));           
			}else{
				holder.txtHQ.setBackgroundColor(mContext.getResources().getColor(R.color.mbx_font_lyric_gray));    
			}
		}else{
			holder.txtHQ.setVisibility(data.isHQ() ? View.VISIBLE : View.GONE);
		}
        if (playId != 0 && playId == data.getId()) {
            holder.vPlay.setVisibility(View.VISIBLE);
        } else {
            holder.vPlay.setVisibility(View.INVISIBLE);
        }
        StarAppUtils.setViewTypeFace(holder.txtHQ, false);
        StarAppUtils.setViewTypeFace(holder.txtMV, false);
        StarAppUtils.setViewTypeFace(holder.txtName, false);
        StarAppUtils.setViewTypeFace(holder.txtSinger, false);
        StarAppUtils.setViewTypeFace(holder.txtTime, false);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        ManagerFactory.getPlayerInstance().playByItem(mContext, getItem(arg2), null, arg2, false);
    }

}

/**
 * 
 * <br>
 * Created 2014年9月15日 下午11:06:47
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
class PlayListViewHolder implements UnMixable {
    TextView txtName;
    TextView txtSinger;
    TextView txtTime;
    TextView txtMV;
    TextView txtHQ;
    View vPlay;
}
